package com.blackhorse.annotation.dao;

import com.blackhorse.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

//注解配置的UserMapper接口
//注意：如果使用注解配置，当dao目录存在mapper的xml文件时，不管是否在SqlMapConfig.xml导入mapper.xml，运行时都会报错

@CacheNamespace(blocking = true)//开启本mapper的二级缓存
public interface AUserMapper {
    @Select("select * from user")
    public List<User> findAll();

    @Select("select * from user where id = #{value}")
    public User findById(int id);

    @Select("select * from user where username like '%${value}%'")
    public List<User> findByName(String name);

    @Select("select count(id) from user")
    public int findTotal();

    @Insert("insert into user(username, birthday, sex, address) values (#{username},#{birthday},#{sex},#{address})")
    public void saveUser(User user);

    @Update("update user set username = #{username}, birthday = #{birthday} , sex = #{sex} , address = #{address} where id = #{id}")
    public void updateUser(User user);

    @Delete("delete from user where id = #{value}")
    public void deleteUserById(int id);

    //resultMap的注解@Results，可以在别的sql注解下使用@ResultMap("userResultMap1")引用这个
    @Select("select id, username , birthday birthday_, sex sex_, address address_ from user")
    @Results(id = "userResultMap1", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "birthday_", property = "birthday"),
            @Result(column = "sex_", property = "sex"),
            @Result(column = "address_", property = "address"),
    })
    public List<User> findAllWithResultMap();

    @Select("select id, username , birthday birthday_, sex sex_, address address_ from user where id = #{value}")
    @ResultMap("userResultMap1")//使用@ResultMap("userResultMap1")引用定义好的@Results
    public User findByIdWithResultMap(int id);


    //一对多关联查询
    @Select("select * from user")
    @Results(id = "userResultMap2", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "birthday", property = "birthday"),
            @Result(column = "sex", property = "sex"),
            @Result(column = "address", property = "address"),
            @Result(property = "accounts", column = "id",
                    many = @Many(select = "com.blackhorse.annotation.dao.AAccountMapper.findByUid", fetchType = FetchType.EAGER))
    })
    public List<User> findAllUserWithAccount();

    //一对多关联查询，使用延迟加载
    @Select("select * from user")
    @Results(id = "userResultMap3", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "birthday", property = "birthday"),
            @Result(column = "sex", property = "sex"),
            @Result(column = "address", property = "address"),
            @Result(property = "accounts", column = "id",
                    many = @Many(select = "com.blackhorse.annotation.dao.AAccountMapper.findByUid", fetchType = FetchType.LAZY))
    })
    public List<User> findAllUserWithAccountByLazyLoading();
}
